<?php
/**
* @property CI_Loader $load
* @property CI_Form_validation $form_validation
* @property CI_Input $input
* @property CI_Email $email
* @property CI_DB_active_record $db
*
*/
class Sde extends CI_Controller
{
    var $id;
    function  __construct()
    {
        parent::__construct();
        $this->id=$this->session->userdata('id');
        $this->load->model("Newconnection");
        $this->load->model("Employee");
        $this->load->model("Applicant");
        $this->load->model("Customer");
        $this->load->model("Shiftconnection");
        $this->load->model("Complain");
        $this->load->helper('form');

    }
   function index()
    {
        $this->entrance();
        $data["title"]="Sub Divisional Engineer";
        $data["content"]="admin/sde/sde_start_content";
        $data["sidebar"]="admin/sde/sde_sidebar";
        $js[]="jquery.validate.js";
        $data["js"]=array('sde_start.js'); 
        $this->load->view('template',$data);
    }

    function entrance()
    {

        if($this->session->userdata('logged_in')!=null && $this->session->userdata('rank')=='SDE' )
        {
            $this->id=$this->session->userdata('id');
            return 1;
        }
        else
        {
            redirect('home');
        }
    }
    function new_connection()
    {
        $this->entrance();
        $query=$this->Newconnection->recently_arrived($this->id,'Forwarded');
        $data["title"]="Sub Divisional Engineer";
        $data["content"]="admin/sde/sde_new_content";
        $data["sidebar"]="admin/sde/sde_sidebar";
        $data["list"]=$query->result_array();
        $data["count"]=$query->num_rows() ;
        $data["js"]=array("sde_new.js");
        $this->load->view('template',$data);
    }
    function detail_ajax()
    {
        $this->entrance();
        $id = $this->input->post('id');
        $data= json_encode($this->Applicant->get_Detail($id));
        echo $data;
    }
    function  new_forward()
    {
       
       $appid= $this->input->post('id');
       $result= $this->input->post('result');
       $cause= $this->input->post('cause');

       $this->db->trans_start();
       $this->Newconnection->forward_report($appid,$result,$cause);
       $this->Newconnection->update_status($appid,'Reported');
       $this->Newconnection->update_message($appid,'Feasibility report for your application is '.$result);
       $this->db->trans_complete();

       redirect('sde/new_connection');
    }
	/***************shift*********************/
     function shift_connection()
    {
        $query=$this->Shiftconnection->recently_arrived($this->id,'Forwarded');
        $data["title"]="Sub Divisional Engineer";
        $data["content"]="admin/sde/sde_shift_content";
        $data["sidebar"]="admin/sde/sde_sidebar";
        $data["list"]=$query->result_array();
        $data["count"]=$query->num_rows() ;
        $data["js"]=array("sde_shift.js");
        $this->load->view('template',$data);
    }
     function shift_detail_ajax()
    {
        $id = $this->input->post('id');
        $data= json_encode($this->Shiftconnection->get_Detail($id));
        echo $data;
    }
     function  shift_forward()
    {

       $appid= $this->input->post('id');
       $result= $this->input->post('result');
       $cause= $this->input->post('cause');
       print_r($_POST);
       $this->db->trans_start();
       $this->Shiftconnection->forward_report($appid,$result,$cause);
       $this->Shiftconnection->update_status($appid,'Reported');
       $this->Shiftconnection->update_message($appid,'Feasibility report for your application is '.$result);
       $this->db->trans_complete();

       redirect('sde/shift_connection');
    }
    /**********************Complain***********************/
    function complain()
    {
        $data["title"]="Sub Divisional Engineer";
        $data["content"]="admin/sde/sde_complain_content";
        $data["sidebar"]="admin/sde/sde_sidebar";
        $data["js"]=array("sde_complain.js");
        $this->load->model("Complain");
        $data["category"]=$this->Complain->get_category();
        $this->load->view("template",$data);
       
    }
     function get_complaints_ajax() {
        $category = $_POST['category'];
        $status = $_POST['status'];
        echo json_encode($this->Complain->get_complaints($this->id, $status, $category));
    }
    function get_action() {
        $appid = $_POST['appid'];
        echo json_encode($this->Complain->get_action($appid));
    }
    function add_action_ajax() {
       // print_r($_POST);
        $desc=$this->input->post('desc');
        $id = $this->input->post('appid');
        echo json_encode($this->Complain->add_action($id,$desc));
        

    }
    function solved() {
         //$category = $_POST['category'];
        //print_r($_POST);
        $id=$this->input->post('id');
        $this->Complain->update_status($id,'Solved');
        $this->Complain->update_message($id,'Your Complaint has been solved on '.date("Y-m-d"));
        $this->db->set('accomplishdate','NOW()',false);
        $this->db->where('appid', $id);
        $this->db->update('complain');
        redirect('sde/complain');
       // echo "Status is solved";

    }
    function complain_complete_ajax() {
        $id = $this->input->post("id");
        $this->db->set('enddate', 'NOW()', false);
        $this->db->where('id', $id);
        $this->db->update('action');
        $date = date("Y-m-d");
        echo $date;
    }

}
?>